﻿# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Name:         config.py
# Purpose:      The main frame
#
# Author:       Zhang Kun
#
# Created:      2/21/08
# Copyright:    (c) 2007-2008 HanMingSoft, Inc.
# License:      Apache License v2.0
#----------------------------------------------------------------------------

import wx
import os
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A3, A4, B3, B4, landscape
from reportlab.lib.units import inch
from reportlab.platypus import Spacer, SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

A3P = A3
A3L = landscape(A3)
A4P = A4
A4L = landscape(A4)
B3P = B3
B3L = landscape(B3)
B4P = B4
B4L = landscape(B4)

if wx.Platform == '__WXMSW__':
    from wx.lib.pdfwin import PDFWindow

class PdfPanel(wx.Panel):
    def __init__(self, parent, filename):
        wx.Panel.__init__(self, parent, id=-1)
        self.pdf = None

        sizer = wx.BoxSizer(wx.VERTICAL)
#        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER)
        wx.BeginBusyCursor()
        #print filename
        self.pdf.LoadFile(filename)
        wx.EndBusyCursor()

        sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND)

#        btn = wx.Button(self, wx.NewId(), u"前页")
#        self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
#        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
#
#        btn = wx.Button(self, wx.NewId(), u"后页")
#        self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
#        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
#
#        btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND)
#        sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND)

        self.SetSizer(sizer)
        self.SetAutoLayout(True)

    def OnPrevPageButton(self, event):
        self.pdf.gotoPreviousPage()

    def OnNextPageButton(self, event):
        self.pdf.gotoNextPage()

def RegisterReportFont():
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.cidfonts import UnicodeCIDFont, findCMapFile
    from reportlab.pdfbase.ttfonts import TTFont
    from reportlab.lib import fonts

    pdfmetrics.registerFont(TTFont('simsun', 'simsun.TTC'))
    pdfmetrics.registerFont(TTFont('simhei', 'simhei.TTF'))
    fonts.addMapping('simsun', 0, 0, 'simsun')
    fonts.addMapping('simhei', 1, 0, 'simhei')

class ReportDocTemplate(SimpleDocTemplate):
    def SetTitle(self, title):
        self.title = title

    def onFirstPage(self, canvas, doc):
        canvas.saveState()
        if self.title:
            canvas.setFont('simhei',16)
            canvas.drawCentredString(inch, 1.75 * inch, self.title)
        canvas.setFont('simsun',9)
        canvas.drawString(inch, 0.75 * inch, "Page: %d" % doc.page)
        canvas.restoreState()

    def onLaterPages(self, canvas, doc):
        canvas.saveState()
        canvas.setFont('simsun',9)
        canvas.drawString(inch, 0.75 * inch, "Page: %d" % doc.page)
        canvas.restoreState()
        
class Report:
    def __init__(self, filename='output.pdf', title=None, size=A4P):
        self.filename = os.path.join(os.path.abspath(os.curdir), filename)
        #print self.filename
        #self.canvas = canvas.Canvas(self.filename)
        self.doc = ReportDocTemplate(self.filename, pagesize=size)
        self.style = getSampleStyleSheet()
        self.title = title
        self.doc.SetTitle(self.title)

    def Draw(self):
        #self.canvas.drawString(100,100,"Hello Python")
        pass

    def Preview(self, parent=None):
        #self.canvas.showPage()
        #self.canvas.save()
        frame = wx.Frame(parent, -1, u"报表预览", size=(1024,768))
        pdfPanel = PdfPanel(frame, self.filename)
        frame.CentreOnParent()
        frame.Show(True)

RegisterReportFont()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    rpt = Report()
    rpt.Draw()
    rpt.Preview()
    app.MainLoop() 